package org.coding.springbootdemowebflux.controller;

import org.coding.springbootdemowebflux.entity.AppUser;
import org.coding.springbootdemowebflux.exception.UserNameNotMatchPasswordException;
import org.coding.springbootdemowebflux.reposity.AppUserReposity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

/**
 * @auth guoxh
 * @date 2020/10/15 9:39 下午
 * @desc
 **/
@CrossOrigin
@RestController
@RequestMapping("/appuser")
public class AppUserController {

    private AppUserReposity appUserReposity;

    public AppUserController(AppUserReposity appUserReposity) {
        this.appUserReposity = appUserReposity;
    }

    @GetMapping("/getById/{id}")
    public Mono<AppUser> appUser(@PathVariable Long id){
        AppUser appUser = new AppUser();
        appUser.setId(1L);
        appUser.setCode("code1");
        appUser.setName("name1");
        return Mono.just(appUser);
    }

    @RequestMapping("/savemvc")
    public Object saveAppUser(){
        AppUser appUser = new AppUser();
        appUser.setCode("zhangsan");
        return appUserReposity.save(appUser);
    }

    public Mono<ServerResponse> saveAppUserFlux(){
        AppUser appUser = new AppUser();
        appUser.setCode("zhangsan");
        return ServerResponse.ok().body(Mono.just(appUserReposity.save(appUser)),Boolean.class);
    }

    @RequestMapping("/error")
    public Object apperror(){
        throw new UserNameNotMatchPasswordException();
    }
}
